package com.ruoyi.wlxz.service.impl;
import com.ruoyi.wlxz.domain.vo.ChatDateCount;
import com.ruoyi.wlxz.domain.Historychat;
import com.ruoyi.wlxz.mapper.HistorychatMapper;
import com.ruoyi.wlxz.service.IHistorychatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * 历史对话Service业务层处理
 *
 * @author zhanghao
 * @date 2025-03-07
 */
@Service
public class HistorychatServiceImpl implements IHistorychatService {
    @Autowired
    private HistorychatMapper historychatMapper;
    /**
     * 查询历史对话
     *
     * @param id 历史对话主键
     * @return 历史对话
     */
    @Override
    public Historychat selectHistorychatById(Long id) {
        return historychatMapper.selectHistorychatById(id);
    }
    /**
     * 查询历史对话列表
     *
     * @param historychat 历史对话
     * @return 历史对话
     */
    @Override
    public List<Historychat> selectHistorychatList(Historychat historychat) {
        return historychatMapper.selectHistorychatList(historychat);
    }
    /**
     * 新增历史对话
     *
     * @param historychat 历史对话
     * @return 结果
     */
    @Override
    public int insertHistorychat(Historychat historychat) {
        return historychatMapper.insertHistorychat(historychat);
    }
    /**
     * 修改历史对话
     *
     * @param historychat 历史对话
     * @return 结果
     */
    @Override
    public int updateHistorychat(Historychat historychat) {
        return historychatMapper.updateHistorychat(historychat);
    }
    /**
     * 批量删除历史对话
     *
     * @param ids 需要删除的历史对话主键
     * @return 结果
     */
    @Override
    public int deleteHistorychatByIds(Long[] ids) {
        return historychatMapper.deleteHistorychatByIds(ids);
    }
    /**
     * 删除历史对话信息
     *
     * @param id 历史对话主键
     * @return 结果
     */
    @Override
    public int deleteHistorychatById(Long id) {
        return historychatMapper.deleteHistorychatById(id);
    }
    @Override
    public int deleteByChatId(String chatId) {
        return historychatMapper.deleteByChatId(chatId);
    }
    @Override
    public List<ChatDateCount> getHistoryChatByData(int days,int deptId) {
        LocalDate startDate = LocalDate.now().minusDays(days-1);
        LocalDate endDate = LocalDate.now();
        List<Historychat> historyChatByData;
        // 获取指定日期范围内的所有聊天记录
        if (deptId!=0){
            //查询不同部门
            historyChatByData = historychatMapper.getHistoryChatByData(startDate,deptId);
        }else {
            //查询全部部门
             historyChatByData = historychatMapper.getHistoryChatAll(startDate);
        }
        // 按日期分组并统计每天的聊天数量
        Map<LocalDate, Long> chatCountByDate = historyChatByData.stream()
                .collect(Collectors.groupingBy(
                        chat -> chat.getTime().toLocalDate(),
                        Collectors.counting()
                ));
        // 创建结果列表，包含从开始日期到结束日期的每一天
        List<ChatDateCount> result = new ArrayList<>();
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            ChatDateCount dateCount = new ChatDateCount();
            dateCount.setDate(date);
            dateCount.setCount(chatCountByDate.getOrDefault(date, 0L).intValue());
            result.add(dateCount);
        }
        return result;
    }
    @Override
    public List<String> getWordCloudByData(int days) {
        LocalDate startDate = LocalDate.now().minusDays(days-1);
        return historychatMapper.getWordCloudByData(startDate);
    }
}
